BINDING_HEADER_CCR = "_ccR"
BINDING_NAME_CCR_TOGGLE = "Start/Stop _ccR"
BINDING_NAME_AUTO_AOE_TOGGLE = "Toggle Auto-AoE"
BINDING_NAME_MANUAL_AOE_TOGGLE = "Manual AoE On/Off"

--[[
	Minimap code stolen from; http://www.wowinterface.com/forums/showthread.php?t=2762
	www.fh-wow.com/firehack/API.lua
]]--
ccR_Settings = {
	MinimapPos = 45
}
function ccR_MinimapButton_Reposition()
	ccR_MinimapButton:SetPoint("TOPLEFT","Minimap","TOPLEFT",52-(80*cos(ccR_Settings.MinimapPos)),(80*sin(ccR_Settings.MinimapPos))-52)
end

function ccR_MinimapButton_DraggingFrame_OnUpdate()
	local xpos,ypos = GetCursorPosition()
	local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
	xpos = xmin-xpos/UIParent:GetScale()+70 
	ypos = ypos/UIParent:GetScale()-ymin-70
	ccR_Settings.MinimapPos = math.deg(math.atan2(ypos,xpos))
	ccR_MinimapButton_Reposition()
end

function ccR_Initialize()
	if not ccRF then
		ccRF = CreateFrame("Frame");
		ccRF:SetFrameStrata("TOOLTIP");
		ccRF:Show();
		ccRF:SetWidth(1);
		ccRF:SetHeight(1);
		ccRF:RegisterEvent("PLAYER_ENTERING_WORLD")
		ccRF:RegisterEvent("ADDON_LOADED")
		ccRF:SetScript("OnEvent", ccR_OnEvent)
		ccRF:SetScript("OnUpdate", ccR_OnUpdate);
	end
end

function ccrStartStop()
	RunMacroText("/ccr rotation")
end
function CopyDefaults(src, dst)
	if type(src) ~= "table" then return {} end
	if type(dst) ~= "table" then dst = {} end
	for k, v in pairs(src) do
		if type(v) == "table" then
			dst[k] = CopyDefaults(v, dst[k])
		elseif type(v) ~= type(dst[k]) then
			dst[k] = v
		end
	end
	return dst
end
function ccR_OnEvent(self, event, arg1)
	if event == "ADDON_LOADED" then
		ccRS = CopyDefaults(ccR_Default, ccRS)
		PrintToChat("Loaded")
		ccRF:UnregisterEvent("ADDON_LOADED")
	end
	if event == "PLAYER_ENTERING_WORLD" then

		if not groupcomposereditbox then GroupComposerFrame() end
		if not ccRS.ClassPlayer or ccRS.ClassPlayer ~= select(3, UnitClass("player")) then ccRS.ClassPlayer = select(3, UnitClass("player")) end
	end
end

local vTempFrameCount = 0
function ccR_OnUpdate(self, elapsed)
	if ccRS.Enabled
	and FireHack
	then
		-- Background Working Stuff, Run on first frame
		if not ccrRunOnce then
			vTempFrameCount = vTempFrameCount + 1
		end
		if vTempFrameCount > 0
		and FireHack
		and not ccrRunOnce
		then
			ccR_RunOnce()
		end;

		-- Run the rotations
		if ccRS.RunPerFrameCount then
			if ccRS.FrameCount == nil then ccRS.FrameCount = 0 end
			ccRS.FrameCount = ccRS.FrameCount + 1
			if (ccRS.FrameCount >= ccRS.FrameCountMax) then
				ccRS.FrameCount = 0;
				ccR_RunRotation()
			end
		end
		if ccRS.RunPerTimeCount then
			if ccRS.TimeCount < 1 then ccRS.TimeCount = 1 end
			if (GetTime()-ccRS.TimeCount) > ccRS.TimeCountMax then
				ccRS.TimeCount = GetTime()
				ccR_RunRotation()
			end	
		end
	end
end

function ccR_RunRotation()


  	if buttonyes and buttonyes:IsShown() then
  		ccR:Hide()
  		ccrstart = false
  		return
  	end

	if ccrStart then
		-- If player dies, Disable
		if UnitIsDeadOrGhost("player") then
			ccR:Hide()
			ccrStart = false
			return
		end
		ccR_PreRotation()
		ccR:Show()
	else
		ccR:Hide()
		ccrStart = false
	end

end

function ccR_RunOnce()
	CreateObjectListFrame()					-- sys\functions\ObjectListToTable.lua
	if ccRS.Auto_Loot then AutoLoot() end	-- sys\functions\AutoLoot.lua
	ccR_Custom_Features()
	ccrRunOnce = true
	RequestPage("https://code.google.com/p/fh-ccr/wiki/UPDATECHECKER?ts=1407487875&updated=UPDATECHECKER", ccR_Update)
	if ccRS.G1zStarTimelessIsle then print("TimelessIsleBuffs script loaded.") SetTimerCallback(G1zStarTimelessIsleBuffsUpTime,  50) ccRS.G1zStarTimelessIsle = true end
	if ccRS.G1zStarSlowfall then print("Slowfall script loaded.") SetTimerCallback(G1zStarSlowfallfunc, 50) ccRS.G1zStarSlowfall = true end
end

function ccRCMD(msg,editbox)
	local command, rest = msg:match("^(%S*)%s*(.-)$");
	command = string.lower(command);
	if msg == "" then
		InterfaceOptionsFrame_OpenToCategory("ccR")
	end
	if command == "on" then
		PrintToChat("Enabled");
		ccRS.Enabled = true
	end
	if command == "off" then
		PrintToChat("Disabled");
		ccRS.Enabled = false
		ccrStart = false
	end
	if command == "autoaoe" then
		if ccRS.Auto_AoE then
			ccRS.Auto_AoE = false
			PrintToChat("Auto-AoE disabled")
		else
			ccRS.Auto_AoE = true
			ccRS.Manual_AoE = false
			PrintToChat("Auto-AoE enabled.")
		end
	end	
	if command == "rotation" then
		ccrStart = not ccrStart;
	end
	if command == "aoe" then
		if ccRS.Manual_AoE then
			PrintToChat("Single Target Mode")
			ccRS.Manual_AoE = false
		else
			PrintToChat("AoE Mode")
			ccRS.Manual_AoE = true
		end
	end
	if command == "debug" then
		if ccRS.Debug then
			PrintToChat("Debug disabled")
			ccRS.Debug = false
		else
			PrintToChat("Debug enabled")
			ccRS.Debug = true
		end
	end
	if command == "g1zstar" then if ccRS.UseAnkhyorG1zStar ~= 3 then ccRS.UseAnkhyorG1zStar = 3 end end
	if command == "ankhy" then	if ccRS.UseAnkhyorG1zStar ~= 2 then ccRS.UseAnkhyorG1zStar = 2 end end
end

function ccReloadUI(msg, editbox)
	ReloadUI();
end

function ccR_Track(msg, editbox)
	TrackByName(msg)
end

function ccR_Script_Print(msg, editbox)
	return RunMacroText("/script print(" .. msg .. ")")
end

SLASH_CCRCMD1 = "/ccr";
SlashCmdList["CCRCMD"] = ccRCMD;
SLASH_CCRL1 = "/rl"
SlashCmdList["CCRL"] = ccReloadUI;
SLASH_CCRTR1 = "/tr"
SlashCmdList["CCRTR"] = ccR_Track;
SLASH_CCRPR1 = "/pr"
SlashCmdList["CCRPR"] = ccR_Script_Print;
ccR_Initialize()